package com.wectrls.security;

import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName CustomTokenEnhancer
 * @Description //TODO 自定义令牌增强器
 * @Author 陈建
 * @Date 2019/2/20 15:48
 * @Version 2.0
 */
@Component
public class CustomTokenEnhancer implements TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {

        //TODO 查询数据库，获取用户的相关信息并加入负载，jwt负载尽量保持信息量最小化原则；注意：jwt负载区严禁加载敏感信息

        Map<String, Object> additionalInfo = new HashMap<>();
        additionalInfo.put("organization", "https://www.wectrls.com");
        ((DefaultOAuth2AccessToken) oAuth2AccessToken).setAdditionalInformation(additionalInfo);
        return oAuth2AccessToken;
    }

}
